package ru.ifmo.rybakov.muclumon.pes;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.ifmo.rybakov.muclumon.pes.sr.SequenceRunType;
import ru.ifmo.rybakov.muclumon.pes.sri.SequenceRunInfo;

public class PESHandler {
	
	private static Map<EventType, IEventHandler> mapping;
	
	private static final Logger logger = LoggerFactory.getLogger(PESHandler.class);
	
	public static synchronized void handle(EventType eventType, SequenceRunInfo sequenceRunInfo, SequenceRunType sequenceRun) {
		if (mapping == null) {
			mapping = new HashMap<EventType, IEventHandler>();
			mapping.put(EventType.SequenceStarted, new SequenceStartedEventHandler());
			mapping.put(EventType.StepStarted, new StepStartedEventHandler());
			mapping.put(EventType.StepFinished, new StepFinishedEventHandler());
			mapping.put(EventType.SequenceFinished, new SequenceFinishedEventHandler());
			mapping.put(EventType.SequenceRunFailed, new SequenceRunFailedEventHandler());
		}
		IEventHandler handler = mapping.get(eventType);
		if (handler == null) {
			logger.warn("No event handler found for event {}", eventType);
		} else {
			handler.handle(sequenceRunInfo, sequenceRun);
		}
	}
}
